﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using TanmiaGrp.Basis.Properties;
using TanmiaGrp.GeneralLedger.Core;
using TanmiaGrp.GeneralLedger.Domain;

namespace TanmiaGrp.GeneralLedger.WinApp.Scenarios
{
    public class AccountEditorViewModel : BaseEditorViewModel<Account, AccountDomain>
    {
        private ObservableCollection<Group> groups;

        public ICollectionView ParentAccounts { get; set; }
        public ObservableCollection<Group> Groups
        {
            get { return groups; }
            set
            {
                if (groups != value)
                {
                    groups = value;
                    RaisePropertyChanged(() => Groups);
                }
            }
        }

        public GroupDomain GroupBizDomain { get; set; }

        public AccountEditorViewModel()
        {
            BusinessDomain = AccountDomain.Obj;
            GroupBizDomain = GroupDomain.Obj;

            PageNavigationEvent = Events.AccountPageNavigation;
            BrowserInitializedEvent = Events.AccountBrowserInitialized;
            EntityNavigationEvent = Events.AccountEntityNavigation;
            EntitySavedOrDeletedEvent = Events.AccountEntitySavedOrDeleted;

            Initialize();

            ParentAccounts = CollectionViewSource.GetDefaultView(BusinessDomain.GetNonTransactionalAccounts());
        }

        protected override Account CoreNew()
        {
            if (Entity == null)
                Entity = BusinessDomain.CreateNew();
            else
                Entity = BusinessDomain.CreateNewChild(Entity.Parent);

            Entity.CaptureState();

            return Entity;
        }

        public void RemoveAccountGroup(Group selectedGroup)
        {
            if (Entity.AccountGroups == null)
                Entity.AccountGroups = new ObservableCollection<Group>();

            Entity.AccountGroups.Remove(selectedGroup);
            Groups.Add(selectedGroup);
        }

        public void AddAccountGroup(Group selectedGroup)
        {
            if (Entity.AccountGroups == null)
                Entity.AccountGroups = new ObservableCollection<Group>();

            Entity.AccountGroups.Add(selectedGroup);
            Groups.Remove(selectedGroup);
        }

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);

            if (propertyName == "Entity")
            {
                if (Entity != null)
                    if (Entity.AccountGroups != null)
                    {
                        List<Group> allGroups = GroupBizDomain.GetPages();
                        List<Group> accountGroups = Entity.AccountGroups.ToList();
                        List<Group> result = allGroups.Except(accountGroups).ToList();

                        Groups = new ObservableCollection<Group>(GroupBizDomain.GetPages().Except(Entity.AccountGroups));
                    }
                    else
                        Groups = new ObservableCollection<Group>(GroupBizDomain.GetPages());
            }
        }

        protected override void OnEntityPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnEntityPropertyChanged(sender, e);

            if (e.PropertyName == "Parent")
            {
                if (Entity.IsEmpty)
                {
                    Entity.AccountNumber = BusinessDomain.GetNewAccountNumber(Entity.Parent);
                }
            }
        }
    }
}
